﻿using System;
using System.Collections.ObjectModel;

namespace Transportr
{
    public class FavoriteLinesModel
    {
        public ObservableCollection<LineItemViewModel> Items { get; private set; }
        public bool IsDataLoaded { get; private set; }

        public FavoriteLinesModel()
        {
            this.Items = new ObservableCollection<LineItemViewModel>();
            FavoriteStore.Update += (s) => { BeginUpdate(); };
            this.BeginUpdate();
        }

        public void BeginUpdate()
        {
            IsDataLoaded = false;
            Items.Clear();
            if (FavoriteStore.Valid && FavoriteStore.Store.Lines != null)
            {
                foreach (FavoriteStore.LineDetails l in FavoriteStore.Store.Lines)
                {
                    Items.Add(new LineItemViewModel()
                    {
                        lnId = l.Brief.lnId,
                        LineNumber = l.Brief.lnNumber,
                        Route = String.Format("{0} - {1}", l.Brief.endName1, l.Brief.endName2),
                        LineType = Icon.Vehicle((ServiceModel.VehicleType)l.Brief.ltId)
                    });

                }
                IsDataLoaded = true;
            }
        }
    }
}
